package saas.adapter.sdk.core.context;

import lombok.extern.slf4j.Slf4j;
import saas.adapter.sdk.entity.TenantConversionRelation;

/**
 * @title: TenantConversionRelationHolder
 * @description: 租户转换关系持有者（随线程上下文携带）
 * @author: guan.xin
 * @create: 2021-11-24 15:32
 * @since: JDK8
 */
@Slf4j
public class TenantConversionRelationHolder {

    /**
     * 持有租户转换关系
     */
    private static final ThreadLocal<TenantConversionRelation> THREAD_LOCAL_HOLDER = new ThreadLocal<>();

    public static void set(TenantConversionRelation tenantConversionRelation) {
        THREAD_LOCAL_HOLDER.set(tenantConversionRelation);
        log.info("设置线程上下文租户转换关系，TenantConversionRelation：{}", tenantConversionRelation.toString());
    }

    public static TenantConversionRelation get() {
        TenantConversionRelation tenantConversionRelation = THREAD_LOCAL_HOLDER.get();
        log.info("获取线程上下文租户转换关系，TenantConversionRelation：{}", tenantConversionRelation.toString());
        THREAD_LOCAL_HOLDER.remove();
        return tenantConversionRelation;
    }
}
